package com.dynamic.datasource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dynamic.datasource.annotation.DSTransactional;
import com.dynamic.datasource.annotation.DataSource;
import com.dynamic.datasource.dao.entity.Person;
import com.dynamic.datasource.dao.mapper.PersonMapper;
import com.dynamic.datasource.service.PersonService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-01
 */
@Service
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    @DataSource("datasouece1")
    @Override
    public List<Person> getPerson1() {
        return list();
    }

    @DataSource("datasouece2")
    @Override
    public List<Person> getPerson2() {
        return list();
    }

    @DSTransactional
    @DataSource("datasouece1")
    @Override
    public void save1() {
        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setId(i + 3);
            person.setName("datasource1 : " + i + "号");
            person.setAge(i + 10);
            person.setSex("男");
            person.setAddress("ifs中心");
            save(person);
            if (i == 5) {
                i = 1 / 0;
            }
        }
    }

    @DataSource("datasouece2")
    @Override
    public void save2() {
        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setId(i + 3);
            person.setName("datasource2 : " + i + "号");
            person.setAge(i + 10);
            person.setSex("男");
            person.setAddress("ifs中心");
            save(person);
            if (i == 5) {
                i = 1 / 0;
            }
        }
    }
}
